<template>
    <div class="content">
        <el-row :gutter="20">
            <el-col :span="12">
                <el-button type="primary" :icon="CirclePlus" @click="onEdit(0)">添加</el-button>
            </el-col>
            <el-col :span="12" class="text-right">
                <el-input class="ml5" v-model="form.search" style="width: 240px" placeholder="请输入关键词" />
                <el-button type="primary" class="ml10" :icon="Search" @click="onSearch">搜索</el-button>
                <el-button type="primary" class="ml10" :icon="RefreshRight" @click="onSearch('Refresh')" />
            </el-col>
        </el-row>
        <el-table class="mt20" border :data="tableData" v-loading="form.loading" style="width: 100%;font-size: 16px;">
            <el-table-column type="index" :index="indexMethod" label="序号" width="60" align="center" />
            <el-table-column prop="name" label="单位名称" show-overflow-tooltip align="center" />
            <el-table-column prop="mobile" label="电话" show-overflow-tooltip align="center" />
            <el-table-column prop="legal" label="企业法人" show-overflow-tooltip align="center" />
            <el-table-column prop="legal_mobile" label="企业法人联系电话" show-overflow-tooltip align="center" />
            <el-table-column prop="mobile" label="联系方式" show-overflow-tooltip align="center" />
            <el-table-column fixed="right" label="操作" width="120" align="center">
                <template #default="scope">
                    <el-link type="primary" @click="onEdit(scope.row.id)" >查看</el-link>
                    <component :is="ApplyLink" model = "Supplier" source = "Supplier" type = "delete" :id = scope.row.id />
                </template>
            </el-table-column>
        </el-table>
        <div class="mt20">
            <el-pagination v-model:current-page="form.current" v-model:page-size="form.limit" :page-sizes="[10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper" :total="form.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
    </div>


    <el-dialog v-model="isApplyRecord" title="申请进度" width="50%">
        <component :is="applyRecord" :id = editId />
    </el-dialog>
</template>

<script lang="ts" setup>
import { ref,onMounted } from 'vue'
import router from '../../../router';
import { RefreshRight, Search,CirclePlus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import http from '../../../util/request';
import applyRecord from "@/components/applyRecord.vue";
import ApplyLink from '@/components/applyLink.vue';

const indexMethod = (index: number) => {
  return index+= 1
}

const editId = ref(0)
const isApplyRecord = ref(false)

const form = ref<any>({search:'',page:0,current:0,total:10,limit:20,loading:false,parentBorder:false})
interface Supplier {
    id: number
    title: string
    created_at: string
    start_date: string
    end_date: string
    status: string
}
const tableData = ref<Supplier[]>([])

const onGetData = () => {
    
    form.value.loading = true
    http.post('/supplier',form.value).then((response:any)=>{
        form.value.loading = false
        response.data.data.forEach((e:any) => {
            e.is_apply = true
            if(e.application){
                if(e.application.status == 'approved'){
                    e.is_apply = false
                }
                if(e.application.status == 'pending'){
                    e.is_apply = false
                }
            }
        });
        tableData.value = response.data.data
        form.value.total = response.data.total
    })
}

const onSearch = (type = '') => {
    if(type == 'Refresh'){
        form.value.search = ''
        onGetData()
        return
    }
    if(!form.value.search){
        ElMessage.error('请输入搜索内容')
    }
    form.value.page = 1
    onGetData()
}


const onEdit = (data: number) => {
    router.push({ name: 'supplierEdit',params: { id: data }})
}

const handleSizeChange = (val: number) => {
    form.value.limit = val
    onGetData()
}
const handleCurrentChange = (val: number) => {
    form.value.page = val
    onGetData()
}
onMounted(()=>{
    onGetData()
})

</script>

<style scoped>

</style>